package chapter5.homework.GeometryCalc_Combined;

import java.util.Scanner;
import java.util.InputMismatchException;
public class Main {
    static boolean welcomed=false;
    static boolean choiceSuccess =false;
    static final String[] prompt={"圆","矩形","三角形","退出"};
    static final int CIRCLE=1,RECTANGLE=2,TRIANGLE=3,EXIT=4;
    public static void main(String[] args) {
        Triangle triangle=new Triangle();
        int choice;

        do{
            try{
                choice = show();
                switch (choice) {
                    case EXIT:
                        Main.count();
                        break;
                    case TRIANGLE:
                        triangle.calc();
                        break;
                    case RECTANGLE:
                        Rectangle.calc();
                        break;
                    case CIRCLE:
                        Circle.calc();
                        break;
                }
            }
            catch (InputMismatchException ime){
                System.out.println("请输入数字！");
                choice=0;
            }
        }while (choice!=4);
    }
    static int show(){
        int choice;
        int i;

        Scanner sc=new Scanner(System.in);

        if(welcomed) {
            if(choiceSuccess){
                System.out.println();
            }
            else{
                System.out.println("输入无效，请再试一次");
            }
        }
        else {
            welcomed = true;
            System.out.println("欢迎使用几何计算器");
        }
        System.out.println("请选择以下一项");
        for(i=0;i< prompt.length;i++){
            System.out.print((i+1)+".\t"+prompt[i]+"\t");
        }
        System.out.println();
        try {
            choice = sc.nextInt();
            if (choice <= 4 && choice > 0) {
                choiceSuccess = true;
                return choice;
            } else {
                choiceSuccess = false;
                return show();
            }
        }
        catch (InputMismatchException ime){
            System.out.println("请输入数字！");
            choice=0;
            choiceSuccess=false;
            return show();
        }
    }
    static void count(){
        System.out.println("本次使用共计算了：");
        System.out.println(Circle.getCounter()+"\t个圆");
        System.out.println(Rectangle.getCounter()+"\t个矩形");
        System.out.println(Triangle.getCounter()+"\t个三角形");
        System.out.println("欢迎下次使用！");
    }
}


class Circle {

    public static int getCounter() {
        return counter;
    }

    static int counter;

    static void count() {
        counter++;
    }
    static boolean calc(){
        Scanner scanner=new Scanner(System.in);
        Circle circle;
        double radius;
        while(true){
            try{
                System.out.println("请输入半径");
                radius=scanner.nextDouble();
                if(radius<=0){
                    System.out.println("半径输入无效，请再试一次");
                }
                else{
                    circle=new Circle(radius);
                    circle.showResult();
                    circle.count();
                    return true;
                }
            }
            catch (InputMismatchException ime){
                System.out.println("请输入数字！");
            }
        }
    }

    public final double PI = 3.1415927;
    double radius;

    Circle(double r) {
        this.radius = r;
    }

    double S() {

        return PI * this.radius * this.radius;
    }

    double C() {

        return 2 * PI * this.radius;
    }
    void showResult(){
        System.out.println("该圆周长：\t"+this.C());
        System.out.println("面积：\t\t"+this.S());
    }

}
class Rectangle {
    public static int getCounter() {
        return counter;
    }

    static int counter;

    static void count() {

        counter++;
    }

    double length;
    double width;

    protected static boolean calc(){
        double length,width;
        Scanner scanner=new Scanner(System.in);
        Rectangle rectangle=null;
        while (true){
            try{
                System.out.println("请输入长");
                length = scanner.nextDouble();
                System.out.println("请输入宽");
                width = scanner.nextDouble();
                if (test(length, width)) {
                    rectangle=new Rectangle(length,width);
                    rectangle.showResult();
                    count();
                    return true;
                }
                else {
                    System.out.println("长宽输入无效");
                }
            }
            catch (InputMismatchException ime){
                System.out.println("请输入数字！");
                continue;
            }
        }


    }
    static boolean test(double length,double width){
        if(length<width)return false;
        else if(length<=0)return false;
        else if(width<=0)return false;
        else return true;
    }
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
    public Rectangle(){
        this(0,0);
    }

    double S() {
        return this.width * this.length;
    }
    double C(){
        return 2*(this.length+this.width);

    }
    void showResult(){
        System.out.println("该矩形周长：\t"+this.C());
        System.out.println("面积\t\t"+this.S());
    }
}

class Triangle {
    public static int getCounter() {
        return counter;
    }

    static int counter;
    double a;
    double b;
    public double getA() {return a;}
    public double getB() {return b;}
    public double getC() { return c;}
    public void setA(double a) {
        this.a = a;
    }
    public void setB(double b) {this.b = b;}
    public void setC(double c) {
        this.c = c;
    }
    double c;
    boolean calc(){
        Scanner scanner=new Scanner(System.in);
        int s=0;
        while (true){
            try {
                System.out.println("请输入第" + ++s + "条边的长度");
                this.setA(scanner.nextDouble());
                System.out.println("请输入第" + ++s + "条边的长度");
                this.setB(scanner.nextDouble());
                System.out.println("请输入第" + ++s + "条边的长度");
                this.setC(scanner.nextDouble());
                s=0;
                if (test(this)) {
                    showResult();
                    count();
                    return true;
                }
                else {
                    System.out.println("输入长度无效，请再输一次");
                }
            }
            catch (InputMismatchException ime) {
                System.out.println("请输入数字！");
            }
        }
    }
    static void count() {
        counter++;
    }
    static boolean test(Triangle t) {
        if (t.a<=0) return false;
        else if (t.b<=0) return false;
        else if (t.c<=0) return false;
        else if (t.a >= t.b + t.c) return false;
        else if (t.b >= t.a + t.c) return false;
        else if (t.c >= t.a + t.b) return false;
        else return true;
    }
    double S(){
        double p=(this.a+this.b+this.c)/2;
        return Math.sqrt(p*(p-this.a)*(p-this.b)*(p-this.c));
    }
    double C(){
        return this.a+this.b+ this.c;
    }
    void showResult(){
        System.out.println("该三角形周长：\t"+this.C());
        System.out.println("面积：\t\t"+this.S());
    }
}
